﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using KinSocket;

namespace KinConquer.Map
{
    public class Grid
    {
        public DictionaryV2<Point, DictionaryV2<UInt32, Entities.IMapObject>> mGrid { get; private set; }
        public UInt32 MapID { get; private set; }

        public Grid(UInt32 mapid, UInt32 maxX, UInt32 maxY)
        {
            MapID = mapid;
            mGrid = new DictionaryV2<Point, DictionaryV2<uint, Entities.IMapObject>>();
            /*for (int i = 0; i < maxX; i++)
                for (int j = 0; j < maxY; j++)
                {
                    mGrid.Add(new Point(i, j), new DictionaryV2<uint, Entities.IMapObject>());
                }*/
        }

        public Point GetPoint(Entities.IMapObject o)
        {
            return new Point(o.X, o.Y);
        }

        public void Remove(UInt32 id)
        {
            foreach (KeyValuePair<Point, DictionaryV2<UInt32, Entities.IMapObject>> a in mGrid)
            {
                if (a.Value.ContainsKey(id))
                {
                    mGrid[a.Key].Remove(id);
                }
            }
        }

        public Int32 Add(Entities.IMapObject o)
        {
            if (!mGrid.ContainsKey(GetPoint(o)))
                mGrid.Add(GetPoint(o), new DictionaryV2<uint, Entities.IMapObject>());
            mGrid[GetPoint(o)].Add(o.Identifier, o);
            return 0;
        }

        public Entities.IMapObject Get(UInt32 id)
        {
            return null;
        }

        public List<Entities.IMapObject> Get(UInt32 x, UInt32 y)
        {
            Point p = new Point((int)x, (int)y);
            if (mGrid.ContainsKey(p))
            {
                return new List<Entities.IMapObject>(mGrid[p].EnumerableValues);
            }
            return new List<Entities.IMapObject>();
        }
    }
}
